package com.zhaolang.qogir.ui.adapter.manage;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhaolang.qogir.R;
import com.zhaolang.qogir.bean.ManageProductDetails;
import com.zhaolang.qogir.cache.SharePreCacheHelper;
import com.zhaolang.qogir.ui.manage.ActivityManageAdd;
import com.zhaolang.qogir.ui.manage.ActivityManageShopClassify;
import com.zhaolang.qogir.util.HttpConstant;
import com.zhaolang.qogir.util.SettingsInfoUtil;
import com.zhaolang.qogir.util.Slog;

import java.util.List;

public class ManageProductDetailsAdapter extends BaseAdapter {
    private Context mContext;
    private List<ManageProductDetails> comments;
    private String TAG = "ManageProductDetailsAdapter";
    private ActivityManageShopClassify.CarriageManageListener carriageManageListener;

    public ManageProductDetailsAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setData(List<ManageProductDetails> comments) {
        this.comments = comments;
    }

    public void setCarriageManageListener(ActivityManageShopClassify.CarriageManageListener carriageManageListener){
        this.carriageManageListener = carriageManageListener;
    }

    @Override
    public ManageProductDetails getItem(int position) {
        return comments != null && position >= 0 & position < comments.size() ? comments
                .get(position) : null;
    }

    @Override
    public int getCount() {
        return comments != null ? comments.size() : 0;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        final ManageProductDetails products = getItem(position);
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.manage_product_classify_items, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final String productId = products.getProductId();
        final String classId = products.getProductId();

        if (products.getIvHead().equals("")) {
            holder.ivHead.setBackgroundResource(R.drawable.element_nopic);
        } else {
            String thumbnail = products.getIvHead();
            SettingsInfoUtil.setImageFromUrl(thumbnail, holder.ivHead);
        }

        if (products.getProductSaleMoth().equals("")) {
            holder.manage_product_product_sale_month.setText("0");
        } else {
            holder.manage_product_product_sale_month.setText(products.getProductSaleMoth());
        }

        if (products.getProductStock().equals("")) {
            holder.manage_product_product_stock.setText("0");
        } else {
            holder.manage_product_product_stock.setText(products.getProductStock());
        }

        if (products.getProductName().equals("") || products.getProductName() == null) {
            holder.manage_shop_classify_product_name.setText("未知");
        } else {
            holder.manage_shop_classify_product_name.setText(products.getProductName());
        }

        if (products.getPrice().equals("") || products.getPrice() == null) {
            holder.manage_shop_classify_product_price.setText("未知");
        } else {
            holder.manage_shop_classify_product_price.setText(products.getPrice());
        }

        if(products.getIsOnSale()){
            holder.button_uncarriage.setBackground(mContext.getResources().getDrawable(R.drawable.button_carriage));
        }else{
            holder.button_uncarriage.setBackground(mContext.getResources().getDrawable(R.drawable.button_uncarriage));
        }

        holder.manage_product_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(mContext, ActivityManageAdd.class);
                Bundle bundle = new Bundle();
                bundle.putString("type", "pro_edit");
                bundle.putString("product_id", productId);
                bundle.putString("class_id", classId);
                intent.putExtra("Message", bundle);
                mContext.startActivity(intent);
            }
        });

        final String userId = SharePreCacheHelper.getUserID(mContext);

        holder.button_uncarriage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean isCarriage;
                if (products.getIsOnSale()) {
                    isCarriage = false;
                    products.setIsOnSale(false);
                    holder.button_uncarriage.setBackground(mContext.getResources().getDrawable(R.drawable.button_uncarriage));
                } else {
                    isCarriage = true;
                    products.setIsOnSale(true);
                    holder.button_uncarriage.setBackground(mContext.getResources().getDrawable(R.drawable.button_carriage));
                }
                if(carriageManageListener != null){
                    carriageManageListener.onCarriage(productId, userId, isCarriage);
                }
            }
        });

        return convertView;
    }

    public class ViewHolder {
        private ImageView ivHead;
        private TextView manage_shop_classify_product_name;
        private TextView manage_product_product_sale_month;
        private TextView manage_product_product_stock;
        private TextView manage_shop_classify_product_price;
        private TextView manage_product_edit;
        private TextView button_uncarriage;

        ViewHolder(View view) {
            ivHead = view.findViewById(R.id.iv_head);
            manage_product_edit = view.findViewById(R.id.manage_product_edit);
            button_uncarriage = view.findViewById(R.id.button_uncarriage);
            manage_shop_classify_product_name = view.findViewById(R.id.manage_shop_classify_product_name);
            manage_product_product_sale_month = view.findViewById(R.id.manage_product_product_sale_month);
            manage_product_product_stock = view.findViewById(R.id.manage_product_product_stock);
            manage_shop_classify_product_price = view.findViewById(R.id.manage_shop_classify_product_price);

        }
    }
}
